package edu.hubu.book.service.impl;

import edu.hubu.book.dao.IBookClassDao;
import edu.hubu.book.domain.BookClass;
import edu.hubu.book.service.IBookClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("bookClassService")
public class BookClassServiceImpl implements IBookClassService {

    @Autowired private IBookClassDao bookClassDao;

    @Override
    public List<BookClass> findAll() {
        System.out.println("调用业务层findAll方法");
        return bookClassDao.findAll();
    }

    @Override
    public int addFirstClass(BookClass bookClass) {
        System.out.println("调用业务层添加一级分类");
        try {
            return bookClassDao.addFirstClass(bookClass.getFirstClassName(), bookClass.getDescription());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public boolean findById(Integer firstId) {
        System.out.println("调用业务层查询是否存在一级分类");
        try {
            int row = bookClassDao.findById(firstId);
            if (row == 1) {
                System.out.println("查询成功！存在该一级分类");
                return true;
            } else {
                System.out.println("不存在该一级分类！");
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    public int addSecondClass(BookClass bookClass) {
        try {
            return bookClassDao.addSecondClass(bookClass.getFirstId(), bookClass.getSecondClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public int updateFirstClass(BookClass bookClass) {
        try {
            return bookClassDao.updateFirstClass(bookClass.getSid(), bookClass.getFirstClassName(), bookClass.getDescription());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public int updateSecondClass(BookClass bookClass) {
        try {
            System.out.println("调用业务层修改方法");

            System.out.println("业务层修改成功");
            return bookClassDao.updateSecondClass(bookClass.getSsid(), bookClass.getFirstId(), bookClass.getSecondClassName());
        } catch (Exception e) {
            throw new RuntimeException("修改失败");
        }
    }

    public int deleteFirstClass(BookClass bookClass) {
        try {
            //删除一级分类
            return bookClassDao.deleteFirstClass(bookClass.getFirstClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteSecondClass(BookClass bookClass) {
        try {
            //删除一级分类
            System.out.println("正在删除二级分类···");
            int isDelete = bookClassDao.deleteSecondClass(bookClass.getSecondClassName());
            System.out.println("二级分类删除成功！");
            return isDelete;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
